#include "CFpsController.h"

namespace GameTutor
{
	CFpsController* CFpsController::s_pInstance = 0;

	void CFpsController::SetLimitFrame(unsigned int limit)
	{
		m_iLimitFps = limit;
		m_iLimitFrameDt = 1000/limit;
	}

	void CFpsController::BeginCounter()
	{
		m_iStartTime = CDevice::GetInstance()->GetTimer();
	}

	void CFpsController::EndCounter()
	{
		long EndTime = CDevice::GetInstance()->GetTimer();
		int Dt = int(EndTime - m_iStartTime);
		if (Dt < m_iLimitFrameDt)
		{
			m_iFrameDt = m_iLimitFrameDt;
			CDevice::GetInstance()->SleepEx(m_iLimitFrameDt - Dt);
		} 
		else
		{
			m_iFrameDt = Dt;
			CDevice::GetInstance()->SleepEx(1);
		}
	}
}